package com.tencent.sr.iris.basic.saas.service.dependency;

import com.alibaba.fastjson.JSON;
import com.tencent.sr.iris.basic.saas.common.utils.AssertUtils;
import com.tencent.sr.iris.user.core.service.interfaces.client.organ.OrganBatchUpdateClient;
import com.tencent.sr.iris.user.core.service.interfaces.client.organ.OrganManagerV2Client;
import com.tencent.sr.iris.user.core.service.interfaces.request.organ.IrisOrganItemRequest;
import com.tencent.sr.iris.user.core.service.interfaces.request.organ.OrgUpdateUserRequest;
import com.tencent.sr.iris.user.core.service.interfaces.request.organ.QueryDqDeptRequest;
import com.tencent.sr.iris.user.core.service.interfaces.response.organ.IrisOrganItemResponse;
import com.tencent.sr.iris.user.core.service.interfaces.response.organ.IrisOrganTreeItem;
import com.tencent.sr.iris.user.core.service.interfaces.response.organ.IrisOrganTreeResponse;
import com.tencent.sr.rmall.common.exception.code.BaseErrorCode;
import com.tencent.sr.rmall.common.exception.code.ErrorCode;
import com.tencent.sr.rmall.common.exception.retail.TRetailBizException;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 行政组织管理接口
 *
 * @author ylongzhang
 */
@Slf4j
@Service
public class OrganManagerDependency {

    @Resource
    private OrganManagerV2Client organManagerV2Client;

    @Resource
    private OrganBatchUpdateClient organBatchUpdateClient;

    public IrisOrganItemResponse getOrganItem(Long orgCode, Long groupId) {
        IrisOrganItemRequest request = new IrisOrganItemRequest();
        request.setOrgCode(orgCode);
        request.setGroupId(groupId);
        HttpResult<IrisOrganItemResponse> res = organManagerV2Client.getOrganItem(request);
        if (res.isSuccess()) {
            return res.getData();
        }
        throw new TRetailBizException(res.getCode(), res.getMsg());
    }

    public IrisOrganItemResponse getNewGroupId(QueryDqDeptRequest request) {
        HttpResult<IrisOrganItemResponse> res = organManagerV2Client.getDqUserDept(request);
        if (!res.isSuccess()) {
            throw new TRetailBizException(res.getCode(), res.getMsg());
        }
        return res.getData();
    }

    public IrisOrganItemResponse getDqDept(Long groupId) {
        QueryDqDeptRequest queryDqDeptRequest = new QueryDqDeptRequest();
        queryDqDeptRequest.setGroupId(groupId);
        HttpResult<IrisOrganItemResponse> res = organManagerV2Client.getDqDept(queryDqDeptRequest);
        if (!res.isSuccess()) {
            throw new TRetailBizException(res.getCode(), res.getMsg());
        }
        return res.getData();
    }

    public void updatNewGroupId(OrgUpdateUserRequest request) {
        HttpResult<Void> res = organBatchUpdateClient.updateUserNewGroup(request);
        if (!res.isSuccess()) {
            throw new TRetailBizException(res.getCode(), res.getMsg());
        }
    }

    public List<IrisOrganTreeItem> queryOrganTree() {
        HttpResult<IrisOrganTreeResponse> res = organManagerV2Client.queryOrganTree();
        
        AssertUtils.isNotNull(res, BaseErrorCode.SYSTEM_ERROR);
        AssertUtils.isTrue(res.isSuccess(), new ErrorCode() {
            @Override
            public String getCode() {
                return res.getCode();
            }
            @Override
            public String getDescription() {
                return res.getMsg();
            }
        });

        IrisOrganTreeResponse response = Optional.ofNullable(res.getData()).orElseGet(IrisOrganTreeResponse::new);

        return Optional.ofNullable(response.getGroupList()).orElse(Collections.emptyList());
    }

    public IrisOrganItemResponse getDqDept(QueryDqDeptRequest request) {
        HttpResult<IrisOrganItemResponse> res = organManagerV2Client.getDqDept(request);
        if (res.isSuccess()) {
            return res.getData();
        }
        log.error("Fail to getDqDept`req={}`res={}", JSON.toJSONString(request), JSON.toJSONString(res));
        throw new TRetailBizException(res.getCode(), res.getMsg());
    }

}
